package com.google.code.chaotic.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.chaotic.model.Admin;
import com.google.code.chaotic.model.Commuter;
import com.google.code.chaotic.model.CommutingHabit;
import com.google.code.chaotic.model.CommutingService;
import com.google.code.chaotic.model.HabitInService;
import com.google.code.chaotic.model.HabitWaypoint;
import com.google.code.chaotic.model.ServiceWaypoint;

//

public class DatastoreControlServlet extends HttpServlet {

	protected void doGet( //
			HttpServletRequest request, HttpServletResponse response) //
			throws ServletException, IOException {

		String methodName = request.getParameter("do");
		try {
			Method method = DatastoreControlServlet.class.getMethod(methodName);
			Object result = method.invoke(this);

			PrintWriter w = response.getWriter();
			Calendar now = GregorianCalendar.getInstance();
			String msg = "<html><title>Datastore Control</title>" //
					+ "<body>"//
					+ "<p>Done successfully!</p>" //
					+ "<p>Timestamp:&nbsp; %1$tc</p>" //
					+ "<p>Result:&nbsp; %2$s</p>" //
					+ "</body></html>";
			w.write(String.format(msg, now, result));

		} catch (Exception e) {
			log(e.toString(), e);
		}
	}

	public void generateSampleAdmins() {
		PersistenceManager pm = PmFactory.get().getPersistenceManager();
		Admin admin = new Admin();
		admin.setFirstName("Mister");
		admin.setLastName("Admin");
		admin.setEmail("the.chaotic.chaos@gmail.com");
		admin.setUsername("admin");
		admin.setPasswordMd5("123");
		pm.makePersistent(admin);
	}

	public boolean clearDatastore() {
		clearServices();
		PersistenceManager pm = PmFactory.get().getPersistenceManager();
		boolean cleared = true;
		cleared = cleared & deleteAll(pm, HabitWaypoint.class);
		cleared = cleared & deleteAll(pm, CommutingHabit.class);
		cleared = cleared & deleteAll(pm, Commuter.class);
		cleared = cleared & deleteAll(pm, Admin.class);
		return cleared;
	}

	public boolean clearServices() {
		PersistenceManager pm = PmFactory.get().getPersistenceManager();
		boolean cleared = true;
		cleared = cleared & deleteAll(pm, HabitInService.class);
		cleared = cleared & deleteAll(pm, ServiceWaypoint.class);
		cleared = cleared & deleteAll(pm, CommutingService.class);
		return cleared;
	}

	public boolean initDatastore() throws Exception {
		boolean cleared = clearDatastore();
		if (cleared)
			generateSampleAdmins();
		return cleared;
	}

	private boolean deleteAll(PersistenceManager pm, Class clazz) {
		List completeList = (List) pm.newQuery(clazz).execute();
		List deletableByGae = completeList.subList(
			0, Math.min(completeList.size(), 500) 
		);
		pm.deletePersistentAll(deletableByGae);
		return deletableByGae.size() == completeList.size();
	}
}
